package com.yequan.leetcode.array.twosum_1;

import java.util.HashMap;
import java.util.Map;

/**
 * @author yequan
 * @date 2021/11/21 20:58
 **/
public class RecodeTwoNum {

    public int[] twoSum1(int[] nums, int target) {
        if (nums == null || nums.length == 0) {
            return null;
        }
        Map<Integer, Integer> targetMap = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int num1 = target - nums[i];
            if (targetMap.containsKey(num1)) {
                return new int[]{i, targetMap.get(num1)};
            }
            targetMap.put(num1, i);
        }
        return null;
    }

    public int[] twoSum2(int[] nums, int target) {
        if (nums == null || nums.length <= 2) {
            return null;
        }
        Map<Integer, Integer> targetMap = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int targetNum = target - nums[i];
            if (targetMap.containsKey(targetNum)) {
                return new int[]{i, targetMap.get(targetNum)};
            }
            targetMap.put(targetNum, i);
        }
        return null;
    }

    public int[] twoSum3(int[] nums, int target) {
        if (nums == null || nums.length < 2) {
            return null;
        }
        Map<Integer, Integer> targetMap = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int targetNum = target - nums[i];
            if (targetMap.containsKey(targetNum)) {
                return new int[]{i, targetMap.get(targetNum)};
            }
            targetMap.put(targetNum, i);
        }
        return nums;
    }

}
